//
//  IndexViewController.m
//  village_ios
//
//  Created by mac on 2019/8/3.
//  Copyright © 2019 com.sw.village. All rights reserved.
//



#import "IndexViewController.h"
#import "MenuCollectionViewCell.h"
#import "NewsCollectionCell.h"
#import "MenuItem.h"
#import "SDCycleScrollView.h"
#import "PartyManagerController.h"
#import "VillageManagerController.h"
#import "HealthManagerController.h"
#import "FinanceManagerController.h"
#import "RentalListController.h"
#import "ArticleListController.h"
#import "ChargeManagerController.h"
#import "FileManager.h"
#import "ArticleModel.h"
#import "ArticleDetailController.h"

@interface IndexViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

//view
@property (weak, nonatomic) IBOutlet UIView *silderView;
@property (weak, nonatomic) IBOutlet UICollectionView *menuCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionView *partyCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionView *villageCollectionView;

//data

@property (strong, nonatomic) NSMutableArray *menuList;
@property (strong, nonatomic) NSMutableArray *partyItemList;
@property (strong, nonatomic) NSMutableArray *villageList;

@end

@implementation IndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupInit];
    [self setupBanner];
}
- (void)setupInit {
    
    
    self.menuList = [NSMutableArray array];
    self.partyItemList = [NSMutableArray array];
    self.villageList = [NSMutableArray array];
    
    //data
    NSArray *indexMenusList = [[MenuManager sharedInstance] menusDictM][@"index"];
    self.menuList = [MenuItem mj_objectArrayWithKeyValuesArray:indexMenusList];

    //party
    NSArray *partylist = [[FileManager sharedInstance] demoDictM][@"index"];
    self.partyItemList = [ArticleModel mj_objectArrayWithKeyValuesArray:partylist];

    //village
    NSArray *villagelist = [[FileManager sharedInstance] demoDictM][@"index"];
    self.villageList = [ArticleModel mj_objectArrayWithKeyValuesArray:villagelist];

    [self.tableView reloadData];
    [self.menuCollectionView reloadData];
    [self.partyCollectionView reloadData];
    [self.villageCollectionView reloadData];

}

- (void)setupBanner {
    
    SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 180)
                                                                             delegate:nil
                                                                     placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
//    cycleScrollView2.titlesGroup = titles;
    cycleScrollView2.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    NSArray *imagesURLStrings = @[@"index_banner"];
    cycleScrollView2.localizationImageNamesGroup = imagesURLStrings;
    [self.silderView addSubview:cycleScrollView2];
    
     
     cycleScrollView2.clickItemOperationBlock = ^(NSInteger index) {
         NSLog(@">>>>>  %ld", (long)index);
     };
    
}


#pragma mark - UITableView

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat h = [super tableView:tableView heightForRowAtIndexPath:indexPath];
    return h;
}


#pragma mark - UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (collectionView == self.menuCollectionView) {
        return self.menuList.count;
    }else if (collectionView == self.partyCollectionView) {
        return self.partyItemList.count;
    }else if (collectionView == self.villageCollectionView) {
        return self.villageList.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView == self.menuCollectionView) {
        MenuCollectionViewCell * cell = (MenuCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCollectionViewCell" forIndexPath:indexPath];
        MenuItem *item = self.menuList[indexPath.row];
        cell.menuItem = item;
        return cell;
    } else if (collectionView == self.partyCollectionView) {
        NewsCollectionCell * cell = (NewsCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"NewsCollectionCellDangjian" forIndexPath:indexPath];
        ArticleModel *item = self.partyItemList[indexPath.row];
        cell.articleModel = item;
        return cell;
    } else if (collectionView == self.villageCollectionView) {
        NewsCollectionCell * cell = (NewsCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"NewsCollectionCellCunwu" forIndexPath:indexPath];
        ArticleModel *item = self.villageList[indexPath.row];
        cell.articleModel = item;
        return cell;
    }
    return nil;
    
   
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    

    if (collectionView == self.menuCollectionView){
        MenuItem *item = [[MenuItem alloc]init];
        item = self.menuList[indexPath.row];
        id vc = nil;
        if([item.title isEqualToString:@"党务管理"]){
            vc = [PartyManagerController instanceWithStoryBoard];
        } else if([item.title isEqualToString:@"村务管理"]){
            vc = [VillageManagerController instanceWithStoryBoard];
        }else if([item.title isEqualToString:@"财务管理"]){
            vc = [FinanceManagerController instanceWithStoryBoard];
        }else if([item.title isEqualToString:@"卫生管理"]){
            vc = [HealthManagerController instanceWithStoryBoard];
        }else if([item.title isEqualToString:@"出租屋管理"]){
            vc = [RentalListController instanceWithStoryBoard];
        }else if([item.title isEqualToString:@"公共通知"]){

            vc = [ArticleListController instanceWithStoryBoard];
            [vc setMenuItem:item];

        }else if([item.title isEqualToString:@"政策法规"]){

            vc = [ArticleListController instanceWithStoryBoard];
            [vc setMenuItem:item];

        }else if([item.title isEqualToString:@"便民服务"]){
            [SYProgressHUD showInfoText:@"等待跳转小程序"];
            return;
        }else if([item.title isEqualToString:@"村情民意"]){
            vc = [ChargeManagerController instanceWithStoryBoard];
        }else if([item.title isEqualToString:@"视频监控"]){
            [SYProgressHUD showInfoText:@"等待上线"];
            return;
        }
        if(vc!=nil){
            [self.navigationController pushViewController:vc animated:YES];
        }

    }else if (collectionView == self.partyCollectionView) {
        ArticleModel * articleModel = self.partyItemList[indexPath.row];
        ArticleDetailController *vc = [ArticleDetailController instanceWithStoryBoard];
        vc.articleModel = articleModel;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (collectionView == self.villageCollectionView) {
        ArticleModel * articleModel = self.villageList[indexPath.row];
        ArticleDetailController *vc = [ArticleDetailController instanceWithStoryBoard];
        vc.articleModel = articleModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
